<template>
  <div class="register">
    <!--卡片视图-->
    <el-card>
      <!--头部-->
      <template #header>
        <div class="card-header" style="text-align: left">
          <span style="color: red">会员注册</span>
          <small>USER REGISTER</small>
        </div>
      </template>
      <!--表单-->
      <div class="form">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          size="mini"
          status-icon
        >
          <!-- 用户名 -->
          <el-form-item label="用户名" prop="mobile">
            <el-input v-model="ruleForm.mobile"></el-input>
          </el-form-item>
          <!--密码-->
          <el-form-item label="密码" prop="encodedPassword">
            <el-input
              type="password"
              v-model="ruleForm.encodedPassword"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="oncePassword">
            <el-input
              type="password"
              v-model="ruleForm.oncePassword"
            ></el-input>
          </el-form-item>
          <!--验证码-->
          <el-form-item prop="captcha" label="验证码">
            <el-input
              prefix-icon="el-icon-picture-outline"
              v-model="ruleForm.captcha"
            ></el-input>
            <sidentify :identifyCode="ver"></sidentify>
            <el-link :underline="false" @click.prevent="refresh">刷新验证码</el-link>
          </el-form-item>
          <el-form-item prop="agree">
            <el-checkbox v-model="ruleForm.agree"
              >我已阅读并同意《注册协议》</el-checkbox
            >
          </el-form-item>
        </el-form>
        <div>
          <span>我已注册账号</span><br />
          <router-link to="/login"
            ><el-button style="width: 200px">立即登录</el-button></router-link
          >
        </div>
      </div>
      <!--尾部-->
      <el-divider></el-divider>
      <div class="card-footer">
        <br />
        <el-button style="width: 300px" @click="onregister('ruleForm', $event)"
          >注册</el-button
        >
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref } from "vue";
import { register } from "@/api/register.js";
import { verification } from "@/api/verification.js";
import Sidentify from "../../components/Sidentify.vue";
export default {
  setup() {
    // 验证码
    let ver = ref("");
    function veri() {
      verification().then((res) => {
        ver.value = res.data.data.captcha;
        console.log(ver);
      });
    }
    veri();
    // 注册
    function refresh() {
      veri();
    }
    return {
      ver,
      refresh,
    };
  },
  components: { Sidentify },
  data() {
    // 确认密码规则
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.encodedPassword !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.encodedPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 注册验证规则
      ruleForm: {
        mobile: "",
        encodedPassword: "",
        oncePassword: "",
        captcha: "",
        // agree: false,
      },
      rules: {
        mobile: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        encodedPassword: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
        oncePassword: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        agree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback();
              } else {
                callback(new Error("请勾选已阅读并同意"));
              }
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    onregister(formName, e) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register(this.ruleForm)
            .then((res) => {
              console.log(res);
              if (res.data.code === 401) {
                this.$message.error(res.data.msg);
                let target = e.target;
                if (target.nodeName == "SPAN") {
                  target = e.target.parentNode;
                }
                target.blur();
                return;
              }
              this.$message({
                message: "注册成功",
                type: "success",
              });
              setTimeout(() => {
                this.$router.push(
                  {
                    path: "/login",
                  },
                  2000
                );
              });
            })
            .catch((err) => {
              console.log(err);
              this.$message.error("注册失败");
            });
        } else {
          let target = e.target;
          if (target.nodeName == "SPAN") {
            target = e.target.parentNode;
          }
          target.blur();
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.register {
  /*背景*/
  background: url("../../assets/image/backgroud.jpeg");
  width: 100%;
  padding-top: 50px;
  padding-bottom: 50px;
}
.el-card {
  margin: 0 auto;
  width: 900px;
}
.card-footer {
  text-align: center;
}
.form {
  display: flex;
  justify-content: space-around;
}
.el-form {
  width: 400px;
}
</style>
